import 'package:flutter/material.dart';
import 'package:flutter_basic/basic/basic_view.dart';
import 'package:flutter_basic/multi_engine/messages.dart';
import 'package:flutter_basic/multi_engine/snake_channel.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../controller.dart';
import 'controller.dart';

class PropertyListPage extends BasicPage<PropertyListPageController>{
  PropertyListPage.bind(controller, {String? tag}) : super.bind(controller, tag: tag);
  @override
  Widget build(BuildContext context) {
    print('build-build:${this.runtimeType}');

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
            onPressed: (){
              if(controller.canBack.value){
                SnakeChannel().pop();
              }else {
                HomeController homeController = Get.find(tag: viewTag);
                homeController.pageController.previousPage(
                    duration: Duration(milliseconds: 200),
                    curve: Curves.ease
                );
              }
            },
            icon: Icon(
              Icons.arrow_back,
            )
        ),
        title: Text('楼盘列表页面'),
        centerTitle: true,
      ),
      body: Container(
        color: Colors.grey,
        child: Obx((){
          return SmartRefresher(
            enablePullDown: true,
            enablePullUp: true,
            controller: controller.refreshController,
            onRefresh: _onRefresh,
            header: WaterDropHeader(),
            onLoading: _onLoading,
            child: ListView.builder(
              itemBuilder: (context, index){
                String info = controller.items[index];
                return PropertyItemWidget(
                  info: info,
                  itemClick: (){
                    Map<String, Object> args = Map();
                    args['type'] = '楼盘详情';
                    var param = CommonParams()
                      ..arguments = args;
                    SnakeChannel().pushNativePage(param)!.then((value){
                      if(value is Map){
                        Map<String, dynamic> resultMap = Map<String, dynamic>.from(value);
                        // controller.result.value = resultMap['name'];
                      }
                    });
                  },
                );
              },
              itemCount: controller.items.length,
            ),
          );
        }),
      ),
    );
  }

  void _onRefresh() async{
    await Future.delayed(Duration(milliseconds: 1000));
    controller.items.value = controller.initialItems;
    controller.refreshController.refreshCompleted();
  }

  void _onLoading() async{
    await Future.delayed(Duration(milliseconds: 1000));
    controller.items.add((controller.items.length + 1).toString());
    controller.refreshController.loadComplete();
  }
}


class PropertyItemWidget extends StatelessWidget{
  final String info;
  final Function? itemClick;
  PropertyItemWidget({required this.info, this.itemClick});
  @override
  Widget build(BuildContext context) {

    return GestureDetector(
      onTap: (){
        this.itemClick?.call();
      },
      child: Container(
        margin: EdgeInsets.only(left: 16, right: 16, top: 5, bottom: 5),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
          border: Border.all(width: 1, color: Colors.blue),
        ),
        height: 80,
        alignment: Alignment.center,
        child: Text(
          this.info,
        ),
      ),
    );
  }
}
